package com.yc.upload;


import com.yc.upload.bean.UploadProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
@ComponentScan("com.yc.upload")
@ConditionalOnProperty(prefix = "upload",value = "enable")
public class UploadConfig implements WebMvcConfigurer {

    @Resource
    UploadProperties uploadProperties;

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        uploadProperties.getMapping().forEach((k,v)->{
            System.out.println(k + ":" + v);
            String webPath = k + "/**";
            String diskPath = "file:" + v +"/";
            registry.addResourceHandler(webPath).addResourceLocations(diskPath);
        });
    }
}
